do
    local pvp = pvp
    local lsm = LibStub("LibSharedMedia-3.0")
 
    --determining screen dimensions is total horseshit
    local res = select(GetCurrentResolution(),GetScreenResolutions())
    local _x,_y = strmatch(res,"(%d+)x(%d+)")
    _x = _x+0
    _y = _y+0
	
	local currentClass = 1
	local currentSpec = nil
	local classes, specs = pvp:GetSpecs()
	

--------------------------------------------------------------------------------------------------	
	function pvp:Validate(_,value)
	--[[
		check if the user has entered a valid string into one of the input boxes
	--]]
		if not pvp:Parse(value) then			
            local err = "Check that your message is enclosed in quotes (\"), " 
			err = err.."the number of string substitions (%s) matches the number of keys, "
			err = err.."and all listed keys are separated by commas.\n"
			pvp:Print("|cffff0000Error!|r")
			pvp:Print(value)
			pvp:Print(err)
			return false
		else
			return true
		end
	end

--------------------------------------------------------------------------------------------------	
	function pvp:Usage(keys)
	--[[
		returns a string describing how to use the input boxes (which there will be a lot of)
	--]]
		local use = "\n|cffffffffMessage in quotes (\") with optional string substitions (%s) "
		use = use.."which will be replaced by the listed keys.|r\n\n"
		use = use.."Available keys:\n|cffffffff"..keys.."|r\n\n"
		use = use.."Append a + to any key for all uppercase or - for all lowercase."
		
		return use
	end
	
--------------------------------------------------------------------------------------------------
    local function defaultOption(mode,order,name,desc)
	--[[
		creates a table of default appearance options
		mode = the battleground name
		order = the order of the table in the options table
		name = what to display in-game
		desc = description of option set
	--]]
  
		--grays out options when battleground is disabled
        local disabled = function()
			if mode == "all" then return false end
			local enabled = pvp.settings[mode].enabled
			if enabled then
				return not pvp.settings[mode].override                  
			else
				return true
			end
		end
      
        if not name then name=mode end
      
        local t = {
            name = name,
            type = 'group',
            desc = desc,
            order = order,            
            args = {
                position = {
                    name = "Position",
                    type = 'group',
					dialogInline = true,
					disabled = disabled,
                    order = 100,
                    args = {
                        x = {
                            name = "Horizontal",                           
                            type = 'range',
                            min = 0,
                            max = _x,
                            step = 1,
                            order = 20,
							get =  function()
								return pvp.settings[mode].position.x
							end,
							set = function(_,value)
								pvp.settings[mode].position.x = value
								pvp:Redraw(mode)
							end
                        },
                        y = {
                            name = "Vertical",
                            type = 'range',
                            min = -_y,
                            max = 0,
                            step = 1,
                            order = 30,
							get = function()
								return pvp.settings[mode].position.y
							end,
							set = function(_,value)
								pvp.settings[mode].position.y = value
								pvp:Redraw(mode)
							end
                        },  
                        lock = {
                            name = "Lock",
                            desc= "Toggle ability to drag frame with mouse.",
                            type = 'toggle',
                            width = 'full',
                            order = 10,
							get = function()
								return pvp.settings[mode].position.lock
							end,
                            set = function(_,value)
                                pvp.settings[mode].position.lock = value
                                pvp:ToggleMove(mode)
                                pvp:Redraw(mode)
                            end,
                        },
                    }
                },
                texture = {
                    name = "Texture",
                    type = 'group',
					dialogInline = true,
					disabled = disabled,
                    order = 200,
                    args = {
                        name = {
                            name = "Name",
                            type = 'select', 
							width = 'full',
                            dialogControl = "LSM30_Statusbar",
                            values = lsm:HashTable("statusbar"),
                            order = 10,
							get = function()
								return pvp.settings[mode].texture.name
							end,
                            set = function(_,value)
                                pvp.settings[mode].texture.name = value
                                pvp.settings[mode].texture.path = lsm:Fetch("statusbar",value)
                                pvp:Redraw(mode)
                            end,                        
                        },              
                        infoColor = {
                            name = "Info color",
                            desc = "The default color for the info portion of the bar when a more appropriate color isn't available.",
                            type = 'color',
                            order = 20,
                            hasAlpha = true,
                            get = function()
                                local color = pvp.settings[mode].texture.infoColor
                                if not color then
                                    return nil
                                else
                                    return color.r, color.g, color.b, (color.a or 1)
                                end
                            end,
                            set = function(_,r,g,b,a)
                                local color = { r=r, g=g, b=b, a=a }
                                pvp.settings[mode].texture.infoColor = color
                                pvp:Redraw(mode)
                            end,
                        },
                        statusColor = {
                            name = "Status color",
                            desc = "The default color for the status portion of the bar when a more appropriate color isn't available.",
                            type = 'color',
                            order = 30,
                            hasAlpha = true,
                            get = function()
                                local color = pvp.settings[mode].texture.statusColor
                                if not color then
                                    return nil
                                else
                                    return color.r, color.g, color.b, (color.a or 1)
                                end
                            end,
                            set = function(_,r,g,b,a)
                                local color = { r=r, g=g, b=b, a=a }
                                pvp.settings[mode].texture.statusColor = color
                                pvp:Redraw(mode)
                            end,
                        }
                    }
                },
                bar = {
                    name = "Dimensions",
                    type = 'group',
					dialogInline = true,
					disabled = disabled,
                    order = 300,                    
                    args = {
                        width = {
                            name = "Width",
                            type = 'range',
                            min = 100,
                            max = 500,
                            step = 1,
                            order = 10,
							get = function()
								return pvp.settings[mode].bar.width
							end,
							set = function(_,value)
								pvp.settings[mode].bar.width = value
								pvp:Redraw(mode)
								pvp:Resize()
							end,
                        },
                        height = {
                            name = "Height",
                            type = 'range',
                            min = 10,
                            max = 50,
                            step = 1,
                            order = 20,
							get = function()
								return pvp.settings[mode].bar.height
							end,
							set = function(p,value)
								pvp.settings[mode].bar.height = value
								pvp:Redraw(mode)
								pvp:Resize()
							end,
                        },
                        padding = {
                            name = "Padding",
                            desc = "Space between bars.",
                            type = 'range',
                            min = 1,
                            max = 10,
                            step = 1,
                            order = 30,
							get = function()
								return pvp.settings[mode].bar.padding
							end,
                            set = function(_,value)
                                pvp.settings[mode].bar.padding = value
                                pvp:Resize()
                            end,
                        }
                    }
                },               
                font = {
                    name = "Font",
                    type = 'group',
					dialogInline = true,
					disabled = disabled,
                    order = 400,
                    args = {
                        name = {
                            name = "Name",
                            type = 'select',
                            dialogControl = "LSM30_Font",
                            values = lsm:HashTable("font"),
                            order = 10,
							get = function()
								return pvp.settings[mode].font.name
							end,
                            set = function(_,value)
                                pvp.settings[mode].font.name = value
                                pvp.settings[mode].font.path = lsm:Fetch('font',value)
                                pvp:Redraw(mode)
                            end,
                        },
                        size = {
                            name = "Size",
                            type = 'range',
                            min = 10,
                            max = 50,
                            step = 1,
                            order = 20,
							get = function()
								return pvp.settings[mode].font.size
							end,
                            set = function(_,value)
                                pvp.settings[mode].font.size = value
                                pvp:Redraw(mode)
                            end,
                        },
                        color = {
                            name = "Color",
                            type = 'color',
                            order = 30,
                            get = function()
                                local color = pvp.settings[mode].font.color
                                return color.r,color.g,color.b
                            end,
                            set = function(_,r,g,b)
                                local color = { r=r, g=g, b=b }
                                pvp.settings[mode].font.color = color
                                pvp:Redraw(mode)
                            end,
                        }
                    }
                },
            }
        }
     
		--options for battlegrounds but not for global
        if mode ~= "all" then
            t.args.enabled = {
                name = "Enabled",
                desc = format("Enable addon for %s.",name),
                width = 'full',
                type = 'toggle',
                order = 1,
                get = function()
                    return pvp.settings[mode].enabled
                end,
                set = function(p,value)
                    pvp.settings[mode].enabled = value
                end,
            }
           
            t.args.features = {
                name = "Features",
                type = 'group',
				dialogInline = true,
                disabled = function()
                    return not pvp.settings[mode].enabled
                end,
                order = 10,   
				args = {}
            }
           
            t.args.appearance = {
                name = "Appearance",
                type = 'group',
				dialogInline = true,
                disabled = function()
                    return not pvp.settings[mode].enabled
                end,
                order = 90,
                args = {
                    override = {
                        name = "Override settings",
                        desc = "Override global settings with battleground specific settings.",                       
                        --width = 'full',
                        type = 'toggle',
                        order = 10,
                        get = function()
                            return pvp.settings[mode].override
                        end,
                        set = function(_,value)
                            pvp.settings[mode].override = value
                            if value then
                                pvp:ToggleMove(mode)
                                pvp:Redraw(mode)                          
                            else
                                pvp:ToggleMove("all")
                                pvp:Redraw("all")
                            end
                            pvp:Resize()
                        end,     
                    },          
                    import = {
                        name = "Import",
                        desc = "Import the global settings.",
                        type = 'execute',
                        order = 20,
                        func = function()
                            local pvp = pvp
                            pvp.settings[mode] = pvp:CopySettings(pvp.settings[mode],pvp.settings.all)
                            pvp:ToggleMove(mode)                      
                            pvp:Redraw(mode)
                            pvp:Resize()
                        end,
                    }
                }
            }
        end
     
        return t
    end

	
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
	
    local options = {
        name = "PvPness2",
        handler = pvp,
        type = 'group',
        childGroups = 'tab',
        args = {
            menu = {
                name = "menu",
                desc = "menu",
                type = 'execute',
                func = function()
                    InterfaceOptionsFrame_OpenToCategory("pvp")
                end,
                guiHidden = true,
                order = 1,
            },      
            --general options
            tab_General = {
                name = "General",
                type = 'group',
                order = 2,
                args = {
                    alpha = {
                        name = "Alpha controls",
                        type = 'group',
						dialogInline = true,						
                        order = 100,                          
                        args = {  
                            combat = {
                                name = "In Combat",
                                desc = "Adjust the visibility of the frame when in combat.",
                                type = 'range',                               
                                min = 0.00,
                                max = 1.00,
                                step = 0.01,
                                order = 10,
								get = function()
									return pvp.settings.alpha.combat
								end,
								set = function(_,value)
									pvp.settings.alpha.combat = value
									pvp:Alpha() --newly added
								end,
                            },
                            out = {
                                name = "Out of Combat",
                                desc = "Adjust the visibility of the frame when not in combat.",
                                type = 'range',                               
                                min = 0.00,
                                max = 1.00,
                                step = 0.01,
                                order = 20,
								get = function()
									return pvp.settings.alpha.out
								end,
								set = function(_,value)
									pvp.settings.alpha.out = value
									pvp:Alpha()
								end
                            },
                            linebreak = {
                                name = '\n',
                                type = 'description',
                                order = 21,
                            },
                            mouse = {
                                name = "Mouseover",
                                desc = "Adjust the visibility of the frame when the cursor is placed over it.",
                                type = 'range',                       
                                min = 0.00,
                                max = 1.00,
                                step = 0.01,
                                order = 30,
								get = function()
									return pvp.settings.alpha.mouse
								end,
								set = function(_,value)
									pvp.settings.alpha.mouse = value
									pvp:Alpha()
								end,
                            },
                            fade = {
                                name = "Mouseover Duration",
                                desc = "The duration Mouseover Alpha will apply after the cursor has left.",
                                type = 'range',
                                min = 0,
                                max = 5,
                                step = 1,
                                order = 40,
								get = function()
									return pvp.settings.alpha.fade
								end,
								set = function(_,value)
									pvp.settings.alpha.fade = value
								end,
                            }
                        }
                    }, 					
                    points = {
                        name = "PvP Currency",
                        type = 'group',
						dialogInline = true,
                        order = 200,
                        args = {
                            showHonor = {
                                name = "Honor",
                                desc = "Show a bar with Honor point progess when not in a battleground.",
                                type = 'toggle',
                                order = 10,
								get = function()
									return pvp.settings.points.showHonor
								end,
								set = function(_,value)
									pvp.settings.points.showHonor = value
									pvp:DefaultDisplay()
								end,
                            },
                            honorMax = {
                                name = "Honor max",
                                desc = "The maximum amount of Honor points to track.",
                                type = 'range',
                                min = 0,
                                max = 4000,
                                step = 1,
                                order = 50,
								get = function()
									return pvp.settings.points.honorMax
								end,
								set = function(_,value)
									pvp.settings.points.honorMax = value
									pvp:DefaultDisplay()
								end,
                            },
                            showWeeklyConq = {
                                name = "Weekly Conquest",
                                desc = "Show a bar with weekly Conquest point progress when not in a battleground.",
                                type = 'toggle',
                                order = 30,
								get = function()
									return pvp.settings.points.showWeeklyConq
								end,
								set = function(_,value)
									pvp.settings.points.showWeeklyConq = value
									pvp:DefaultDisplay()
								end,
                            },
                            showMaxConq = {
                                name = "Total Conquest",
                                desc = "Show a bar with Conquest point progress when not in a battleground.",
                                type = 'toggle',
                                order = 40,
								get = function()
									return pvp.settings.points.showMaxConq
								end,
								set = function(_,value)
									pvp.settings.points.showMaxConq = value
									pvp:DefaultDisplay()
								end,
                            },
                            conqMax = {
                                name = "Conquest max",
                                desc = "The maximum amount of Conquest points to track.",
                                type = 'range',
                                min = 0,
                                max = 4000,
                                step = 1,
                                order = 60,
								get = function()
									return pvp.settings.points.conqMax
								end,
								set = function(_,value)
									pvp.settings.points.conqMax = value
									pvp:DefaultDisplay()
								end,
                            }
                        }
                    },
					broadcast = {
						name = "Broadcast",
						desc = "Send addon messages to this chat channel.\n\n|cffffcc00Note:|r A message will be sent to every channel selected. Selecting more than one channel might be annoying to other players.",
						type = 'multiselect',
						order = 300,
						values = {
							SAY = "Say",
							PARTY = "Party",
							INSTANCE_CHAT = "Battleground",
							RAID = "Raid",
							RAID_WARNING = "Raid warning",
						},
						get = function(_,key)
							if tContains(pvp.settings.broadcast,key) then
								return true
							end
						end,
						set = function(_,key,value)
							local t = pvp.settings.broadcast
							if value then
								if not tContains(t,key) then
									tinsert(t,key)
								end
							else
								if tContains(t,key) then
									for i,p in ipairs(t) do
										if p == key then 
											tremove(t,i)
											break
										end
									end
								end
							end								
						end,								
					},					
                    spy = {
						--spy ui/button options
						--spy button text?
						--mouseover color
						--text color
						--location: left/right
						--realm color
                        name = "Spy",
                        type = 'group',
						dialogInline = true,
                        order = 400,
                        args = {
                            localtext = {
                                name = "Local",
                                desc = "The format of player names when using personal spy report (left click).",
                                type = 'input',
                                width = 'double',
								validate = "Validate",
								usage = pvp:Usage("name, class, spec, altclass, altspec"),
                                order = 10,
                                get = function()
                                    return pvp:Unparse(pvp.settings.spy.localtext)
                                end,
                                set = function(_,value)
                                    pvp.settings.spy.localtext = pvp:Parse(value)
                                end                              
                            },
                            localtest = {
                                name = "Test",
                                desc = "Test the message.",
                                type = 'execute',
                                width = 'half',
                                order = 11,
                                func = function()
                                    local keys = {
                                        ["name"] = "Testwarrior",
                                        ["class"] = "Warrior",
                                        ["spec"] = "Protection",
										["altclass"] = pvp.settings.alt.class["Warrior"],
										["altspec"] = pvp.settings.alt.spec["Warrior"]["Protection"],
                                    }
                                    local msg = pvp:FormatText(pvp.settings.spy.localtext,keys)
                                    pvp:TestInput(msg)
                                end,
                            },
							localreset = {
								name = "Reset",
								desc = "Reset to default.",
								type = 'execute',
								width = 'half',
								order = 12,
								func = function()
									pvp.settings.spy.localtext = {"%s (%s)",{"name","spec"}}
								end
							},
							localcolor = {
								name = "Color",
								desc = "The color to use for the text displayed in the chatframe.",
								type = 'color',
								width = 'full',
								order = 13,
								get = function()
									local color = pvp.settings.spy.color
									if not color then
										return nil
									else
										return color.r, color.g, color.b
									end
                                end,
								set = function(_,r,g,b)
									local color = { r=r, g=g, b=b }
									pvp.settings.spy.color = color
								end
							},
							nl = {
								name = "\n",
								type = 'description',
								order = 19,
							},
							globaltext = {
								name = "Global",
								desc = "The format of player names when broadcasting spy report to chat (right click).",
								type = 'input',
								width = 'double',
								validate = "Validate",
								usage = pvp:Usage("name, class, spec, altclass, altspec"),
								order = 20,
								get = function()
									return pvp:Unparse(pvp.settings.spy.globaltext)
								end,
								set = function(_,value)
									pvp.settings.spy.globaltext = pvp:Parse(value)									
								end
							},
							globaltest = {
								name = "Test",
								desc = "Test the message.",
								type = 'execute',
								width = 'half',
								order = 21,
								func = function()
                                    local keys = {
                                        ["name"] = "Testwarrior",
                                        ["class"] = "Warrior",
                                        ["spec"] = "Protection",
										["altclass"] = pvp.settings.alt.class["Warrior"],
										["altspec"] = pvp.settings.alt.spec["Warrior"]["Protection"],
                                    }
                                    local msg = pvp:FormatText(pvp.settings.spy.globaltext,keys)
                                    pvp:TestInput(msg)
                                end,
							},
							globalreset = {
								name = "Reset",
								desc = "Reset to default.",
								type = 'execute',
								width = 'half',
								order = 22,
								func = function()
									pvp.settings.spy.globaltext = {"%s (%s)",{"name","altspec"}}
								end
							},
							healersonly = {
								name = "Healers only",
								desc = "Only broadcast enemy healers to chat.",
								type = 'toggle',
								order = 30,
								get = function()
									return pvp.settings.spy.healersonly
								end,
								set = function(_,value)
									pvp.settings.spy.healersonly = value
								end
							}							
                        }
                    },					
				    alt = {
						name = "Alternate names",						
						type = 'group',
						dialogInline = true,
						order = 500,
						args = {					
							selection_class = {
								name = "Class",
								desc = "Official name.",
								type = 'select',
								style = 'dropdown',
								values = classes,
								order = 1,
								get = function()
									return currentClass
								end,
								set = function(_,value)
									currentClass = value
									currentSpec = nil
								end                      
							},
							class = {
								name = "Alternate",
								desc = "An alternate value for this class name.\n\nUseful for abbreviations or nicknames when displaying messages in chat.",
								type = 'input',
								order = 10,
								get = function()
									local class = classes[currentClass]
									return pvp.settings.alt.class[class]
								end,
								set = function(_,value)
									local class = classes[currentClass]
									pvp.settings.alt.class[class] = value
								end
							},		
							nl = {
								name = "\n",
								type = 'description',
								order = 11,
							},
							selection_spec = {
								name = "Specialization",
								desc = "Official name.",
								type = 'select',
								style = 'dropdown',
								values = function()
									local class = classes[currentClass]
									return specs[class] 
								end,
								order = 20,
								get = function()
									return currentSpec
								end,
								set = function(_,value)
									currentSpec = value
								end,
							},
							spec = {
								name = "Alternate",
								desc = "An alternate value for this specialization.\n\nUseful for abbreviations or nicknames when displaying messages in chat.",
								type = 'input',
								order = 21,
								get = function()
									local class = classes[currentClass]
									return pvp.settings.alt.spec[class][currentSpec]
								end,
								set = function(_,value)
									local class = classes[currentClass]
									pvp.settings.alt.spec[class][currentSpec] = value
								end,
							}
						}
					}
                  
                  
                    --background frame color/alpha
                    --show frame title
                    --frame title font/size/color
                  
                  
                },              
            },
      
            --per battleground options
            tab_Battlegrounds = {
                name = "Battleground options",
                type = 'group',
                order = 3,
                childGroups = "tree",
                args = {
                    all = defaultOption("all",1,"Global appearance","Global appearance options for all battlegrounds.  These may be overridden for each individual battleground."),
                    wsg = pvp:wsgOptionsTable(defaultOption("wsg",2,"Warsong Gulch/Twin Peaks","Options for Warsong Gulch and Twin Peaks.")),
                }
            }      
        }
    } 
 
    LibStub("AceConfig-3.0"):RegisterOptionsTable("PvPness2", options, {"PvPness2"})
    LibStub("AceConfigDialog-3.0"):AddToBlizOptions("PvPness2")

end --eof 